import { createRouter, createWebHashHistory } from "vue-router";

const routes = [
  {
    path: "/",
    redirect: "/one",
  },
  ,
  {
    path: "/one",
    name: "one",
    component: () => import("./ChildComponent1.vue"),
  },
  {
    path: "/two",
    name: "two",
    component: () => import("./ChildComponent2.vue"),
    children: [
      {
        path: "three",
        name: "three",
        component: () => import("./ChildComponent3.vue"),
        meta: { isAuth: true },
      },
    ],
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.meta.isAuth) {
    if (localStorage.getItem("token") === "1") {
      next();
    } else {
      alert("请先登录");
    }
  } else {
    next();
  }
});

export default router;
